module.exports = function(lab) {
    return {
        @deleteAllMembers: function(identifier) {
            return lab.slime.core.@initSession(identifier).then(function(memberOU) {
                lab.sqlite.find("slime_members", {
                    ouid: memberOU.id
                }).then(function({ records }) {
                    @.async.all(records, function(member) {
                        lab.sqlite.find("slime_member_ids", {
                            main_id: member.id
                        }).then(function({ records }) {
                            @.async.all(records, function(member_id) {
                                lab.delete("slime_member_ids", member_id).pipe(this)
                            }).pipe(this)
                        }).then(function() {
                            lab.delete("slime_members", member).pipe(this);
                        }).then(async function() {
                            process.nextTick(function() {
                                @mew.auto("slime.member_deleted", {
                                    member
                                });
                            })
                        }).pipe(this)
                    }).pipe(this);
                }).pipe(this);
            })
        },
        updateMember: function(session, form) {
            if (!session.currentOU || !session.currentUser) {
                throw @lab.errors.not_login
            }
            return lab.find("slime_members", form.id).then(function(member) {
                let message = ""
                let update_form = {}
                let updated_fields = []
                let item = {
                    "name": "昵称",
                    "real_name": "姓名",
                    "gender": "性别",
                    "country": "国家",
                    "province": "省份",
                    "city": "城市",
                    "district": "区",
                }
                @.keys(item).forEach(function(key) {
                    if (!@.is.nil(form[key]) && member[key] !== form[key]) {
                        message += `${item[key]} 从 ${member[key]} 变更为 ${form[key]}\n`
                        update_form[key] = form[key]
                        updated_fields.push(key)
                    }
                })
                if (!@.is.nil(form.birthday)) {
                    let old_value = @.format.date(new Date(member.birthday), "YYYY-MM-DD")
                    let new_value = @.format.date(new Date(form.birthday), "YYYY-MM-DD")
                    if (old_value !== new_value) {
                        message += `生日 从 ${old_value} 变更为 ${new_value}\n`
                        update_form.birthday = form.birthday
                        updated_fields.push("birthday")
                    }
                }
                if (message) {
                    @mew.auto("lab.add_operator_log", {
                        currentUser: session.currentUser,
                        target: member,
                        target_id: member.id,
                        method: "updateMember",
                        kitty: "slime.admin",
                        type: "会员管理",
                        message: `修改会员信息 ${update_form.name || member.name} ` + message
                    })
                    return lab.slime.member.@updateMember(update_form, {
                        idOwnerOU: session.currentOU,
                        idField: "id",
                        idValue: member.id
                    }).then(function(result) {
                        return lab.create("slime_member_updates", @.merge.simple(update_form, {
                            updated_fields,
                            member_id: member.id,
                            source_id: "",
                            create_date: Date.now()
                        })).then(function() {
                            return lab.elasticsearch_crm.core.@updateMember({
                                id: result.id,
                                doc: update_form,
                                upsert: result
                            }).resolve(result)
                        })
                    })
                } else {
                    this.next(member)
                }
            })
        }
    }
}